package com.xxr.lib_popup.utlis;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.xxr.lib_popup.R;

/**
 * 作者：姜澜
 * 时间：2020/6/9
 * 简述：弹出框父类
 * 邮箱：31679104@qq.om
 */
public class DialogBase extends Dialog {

    public DialogBase(@NonNull Context context) {
        super(context, R.style.Dialog);
    }

    public DialogBase(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected DialogBase(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static abstract class Builder{
        protected Activity mActivity;
        //标题
        protected String title;
        //二级标题
        protected String viceTitle;
        //消息
        protected String message;
        //确认按钮提示文字
        protected String positiveButtonText;
        //取消按钮提示文字
        protected String negativeButtonText;
        //图片资源路径
        protected int imageUrl;
        //布局view
        protected View layoutView;

        protected View contentView;
        //确认按钮点击事件
        protected OnClickListener positiveButtonClickListener;
        //取消按钮点击事件
        protected OnClickListener negativeButtonClickListener;
        //是否允许返回键关闭
        protected boolean isCancelable = true;

       protected DialogBase dialog;

        protected boolean isDialog = true;

        public Builder(Activity activity){
            this.mActivity = activity;
        }
        public Builder(Context context){
            this.mActivity = (Activity) context;
        }

        /**
         * 获取布局文件
         * @return
         */
        protected int getLayout(){
            return 0;
        }
        /**
         * 设置动画
         */
        protected int getAnim(){
            return 0;
        }

        /**
         * 设置布局文件
         */
        protected View getView(float width,float high){
            //设置弹出框布局
            LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            dialog = new DialogBase(mActivity);
            assert inflater != null;
            layoutView = inflater.inflate(getLayout(),null);
            dialog.addContentView(layoutView,new AbsListView.LayoutParams(AbsListView.LayoutParams.FILL_PARENT
                    , AbsListView.LayoutParams.WRAP_CONTENT));



            //设置宽高
            dialog.setContentView(layoutView);
            Window window = dialog.getWindow();
            WindowManager manager = mActivity.getWindowManager();
            Display display = manager.getDefaultDisplay();
            WindowManager.LayoutParams params = window.getAttributes();

            //设置是否允许返回键关闭
            dialog.setCancelable(isCancelable);
            dialog.setCanceledOnTouchOutside(isCancelable);

            if (0!=width){
                params.width = (int) (display.getWidth()*width);
            }
           if (0!=high){
               params.height = (int) (display.getHeight()*high);
           }

            window.setAttributes(params);

            //设置动画
            if (getAnim()!=0){
                window.setWindowAnimations(getAnim());
            }

            return layoutView;
        }


        public final <T extends View> T findViewById(int id){
            return layoutView.findViewById(id);
        }
        /**
         * 绑定控件
         */
        protected abstract void initBind();

        protected abstract void initView();

        /**
         * 启动弹出框
         */
        public void show(){
            //设置逻辑
            initBind();
            initView();
            dialog.show();

        }

        /**
         * 关闭弹出框
         */
        public void dismiss(){
            dialog.dismiss();
        }

        /**
         * 设置标题
         * @param title
         * @return
         */
        public Builder setTitle(String title){
            this.title = title;
            return this;
        }

        /**
         * 设置副标题
         * @param viceTitle
         * @return
         */
        public Builder setViceTitle(String viceTitle){
            this.viceTitle = viceTitle;
            return this;
        }

        /**
         * 设置消息
         * @param message
         * @return
         */
        public Builder setMessage(String message){
            this.message = message;
            return this;
        }

        public Builder setContentView(View v){
            this.contentView = v;
            return this;
        }

        /**
         * 设置点击成功
         * @param positiveButtonText
         * @param listener
         * @return
         */
        public Builder setPositiveButton(String positiveButtonText
                , OnClickListener listener){
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        /**
         * 设置点击取消
         * @param negativeButtonText
         * @param listener
         * @return
         */
        public Builder setNegativeButton(String negativeButtonText
                , OnClickListener listener){
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        /**
         * 设置图片
         * @param imageUrl
         * @return
         */
        public Builder setImage(int imageUrl){
            this.imageUrl = imageUrl;
            return this;
        }

        /**
         * 判断是否需要自动关闭提示框
         * @return
         */
        public Builder isDialogDismiss(boolean dialog) {
            isDialog = dialog;
            return this;
        }

        /**
         * 设置是否允许返回键关闭
         * 默认允许
         */
        public Builder setCancelable(boolean flag){
            dialog.setCancelable(flag);
            dialog.setCanceledOnTouchOutside(flag);
            return this;
        }
    }
}
